package com.baihan.angus.app.game.basic.base;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.ContextThemeWrapper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.R;
import com.baihan.angus.app.game.basic.base.view_model.BaseMldEvent;
import com.baihan.angus.app.game.basic.base.view_model.BaseViewModel;
import com.baihan.angus.app.game.basic.configs.BasicConfig;
import com.baihan.angus.app.game.basic.utils.ActivityManager;
import com.baihan.angus.app.game.basic.utils.AppLanguageUtil;
import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.basic.views.LoadingDialog;
import com.gyf.immersionbar.ImmersionBar;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

public abstract class BaseActivity<VDB extends ViewDataBinding, VM extends BaseViewModel> extends RxAppCompatActivity {

    public static final String SHOW_LOADING = "show_loading";
    public static final String HIDE_LOADING = "hide_loading";
    public static final String FINISH = "finish";
    public static final String FINISH_WITH_RESULT = "finish_with_result";

    protected VDB mVDBinding;
    protected VM mViewModel;
    protected LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.getInstance().pushOneActivity(this);
        initDayMode();
        initStatusBar();
        mVDBinding = DataBindingUtil.setContentView(this, getContentLayout());
        initVM();
        //注册
        ARouter.getInstance().inject(this);
        initIntentData();
        initView(savedInstanceState);
        initObservers();
        initData();
    }


    @Override
    protected void attachBaseContext(Context newBase) {
        //获取我们存储的语言环境 比如 "en","zh",等等
        int languageCode = BasicConfig.getInstance().getLanguageCode();
        Context newBaseContext = AppLanguageUtil.attachBaseContext(newBase, languageCode);
        final Configuration configuration = newBaseContext.getResources().getConfiguration();
        // 此处的ContextThemeWrapper是androidx.appcompat.view包下的
        // 你也可以使用android.view.ContextThemeWrapper，但是使用该对象最低只兼容到API 17
        // 所以使用 androidx.appcompat.view.ContextThemeWrapper省心  appcompat:1.2.0 与 appcompat:1.1.0
        final ContextThemeWrapper wrappedContext = new ContextThemeWrapper(newBaseContext,
                R.style.Theme_AppCompat_Empty) {
            @Override
            public void applyOverrideConfiguration(Configuration overrideConfiguration) {
                if (overrideConfiguration != null) {
                    overrideConfiguration.setTo(configuration);
                }
                super.applyOverrideConfiguration(overrideConfiguration);
            }
        };
        /**
         * attach对应语言环境下的context
         */
        super.attachBaseContext(wrappedContext);
    }

    protected void initVM() {
        mViewModel = (VM) new ViewModelProvider(getViewModelStore(), ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(ComUtil.getClassT(BaseActivity.this, 1));
        mVDBinding.setVariable(getViewModelId(), mViewModel);
        mViewModel.initLifecycleProvider(this);
        getLifecycle().addObserver(mViewModel);
    }

    protected void initStatusBar() {
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.bg4).statusBarDarkFont(BasicConfig.getInstance().isDayMode()).init();
    }

    @Override
    protected void onDestroy() {
        if (mVDBinding != null) {
            mVDBinding.unbind();
        }
        ActivityManager.getInstance().popOneActivity(this);
        if (mViewModel != null) {
            getLifecycle().removeObserver(mViewModel);
        }
        super.onDestroy();
    }


    protected abstract int getContentLayout();//获取内容布局

    protected abstract int getViewModelId();//获取ViewModel的BR ID

    //初始化Intent里的数据
    protected void initIntentData() {
    }

    protected abstract void initView(Bundle savedInstanceState);//初始化界面控件

    protected void initObservers() {
        if (mViewModel==null) return;
        mViewModel.getBaseMld().observe(this, new Observer<BaseMldEvent>() {
            @Override
            public void onChanged(BaseMldEvent baseMldEvent) {
                switch (baseMldEvent.action) {
                    case SHOW_LOADING:
                        showLoading(baseMldEvent.stringMsg);
                        break;
                    case HIDE_LOADING:
                        hideLoading(baseMldEvent.stringMsg);
                        break;
                    case FINISH:
                        finish();
                        break;
                    case FINISH_WITH_RESULT:
                        setResult(baseMldEvent.intMsg);
                        finish();
                        break;
                }
            }
        });
    }

    protected void initData() {
    }

    public void showLoading(String tag) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.showDialog(tag);
    }

    public void hideLoading(String tag) {
        if (mLoadingDialog != null) {
            mLoadingDialog.hideDialog(tag);
        }
    }

    /**
     * 初始化 dayMode
     */
    protected void initDayMode() {
        getDelegate().setLocalNightMode(BasicConfig.getInstance().isDayMode()
                ? AppCompatDelegate.MODE_NIGHT_NO : AppCompatDelegate.MODE_NIGHT_YES);
    }
}
